<?xml version="1.0"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html><head><title>SASAPI</title><script src="../../../quicknav.js" type="text/javascript"></script><script><!--
document.quicknavBasePath="../../../index-files";
//--></script><link href="../../../style.css" title="JavaDoc" rel="stylesheet" type="text/css"/><link href="../../../unnatural.css" title="Unnatural" rel="alternate stylesheet" type="text/css"/><link href="../../../overview-summary.html" title="Overview" rel="top"/><meta name="generator" content="http://www.badgers-in-foil.co.uk/projects/as2api/"/></head><body><pre><span class="lineno" id="1">     1  </span><span class="comment doc">/**
<span class="lineno" id="2">     2  </span> * com.sekati.crypt.Base64
<span class="lineno" id="3">     3  </span> * @version 3.0.1
<span class="lineno" id="4">     4  </span> * @author jason m horwitz | sekati.com 
<span class="lineno" id="5">     5  </span> * Copyright (C) 2007  jason m horwitz, Sekat LLC. All Rights Reserved.
<span class="lineno" id="6">     6  </span> * Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
<span class="lineno" id="7">     7  </span> * 
<span class="lineno" id="8">     8  </span> * Sourced from ascrypt for dependencies only - version 2.0, author Mika Pamu
<span class="lineno" id="9">     9  </span> * Original Javascript implementation:
<span class="lineno" id="10">    10  </span> * Aardwulf Systems, www.aardwulf.com
<span class="lineno" id="11">    11  </span> * @see http://www.aardwulf.com/tutor/base64/base64.html 
<span class="lineno" id="12">    12  </span> */</span>
<span class="lineno" id="13">    13  </span>
<span class="lineno" id="14">    14  </span><span class="key">import</span> com.sekati.crypt.ICipher;
<span class="lineno" id="15">    15  </span><span class="lineno" id="16">    16  </span><span class="comment doc">/**
<span class="lineno" id="17">    17  </span> * Encodes and decodes a base64 string.
<span class="lineno" id="18">    18  </span> */</span>
<span class="lineno" id="19">    19  </span><span class="key">class</span> com.sekati.crypt.Base64 <span class="key">implements</span> ICipher {
<span class="lineno" id="20">    20  </span><span class="lineno" id="21">    21  </span>	<span class="key">private</span> <span class="key">static</span> <span class="key">var</span> base64chars:String = <span class="str_const">"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="</span>;
<span class="lineno" id="22">    22  </span><span class="lineno" id="23">    23  </span>	<span class="comment doc">/**
<span class="lineno" id="24">    24  </span>	 * Encodes a base64 string.
<span class="lineno" id="25">    25  </span>	 * @param src (String) - string to encode
<span class="lineno" id="26">    26  </span>	 * @return String
<span class="lineno" id="27">    27  </span>	 */</span>
<span class="lineno" id="28">    28  </span>	<span class="key">public</span> <span class="key">static</span> <span class="key">function</span> encode(src:String):String {
<span class="lineno" id="29">    29  </span>		<span class="key">var</span> i:Number = <span class="num_const">0</span>;
<span class="lineno" id="30">    30  </span>		<span class="key">var</span> output:String = <span class="key">new</span> String( <span class="str_const">""</span> );
<span class="lineno" id="31">    31  </span>		<span class="key">var</span> chr1:Number, chr2:Number, chr3:Number;
<span class="lineno" id="32">    32  </span>		<span class="key">var</span> enc1:Number, enc2:Number, enc3:Number, enc4:Number;
<span class="lineno" id="33">    33  </span>		<span class="key">while</span> (i &lt; src.length) {
<span class="lineno" id="34">    34  </span>			chr1 = src.charCodeAt( i++ );
<span class="lineno" id="35">    35  </span>			chr2 = src.charCodeAt( i++ );
<span class="lineno" id="36">    36  </span>			chr3 = src.charCodeAt( i++ );
<span class="lineno" id="37">    37  </span>			enc1 = chr1 &gt;&gt; <span class="num_const">2</span>;
<span class="lineno" id="38">    38  </span>			enc2 = ((chr1 &amp; <span class="num_const">3</span>) &lt;&lt; <span class="num_const">4</span>) | (chr2 &gt;&gt; <span class="num_const">4</span>);
<span class="lineno" id="39">    39  </span>			enc3 = ((chr2 &amp; <span class="num_const">15</span>) &lt;&lt; <span class="num_const">2</span>) | (chr3 &gt;&gt; <span class="num_const">6</span>);
<span class="lineno" id="40">    40  </span>			enc4 = chr3 &amp; <span class="num_const">63</span>;
<span class="lineno" id="41">    41  </span>			<span class="key">if</span>(isNaN( chr2 )) enc3 = enc4 = <span class="num_const">64</span>;
<span class="lineno" id="42">    42  </span>			<span class="key">else</span> <span class="key">if</span>(isNaN( chr3 )) enc4 = <span class="num_const">64</span>;
<span class="lineno" id="43">    43  </span>			output += base64chars.charAt( enc1 ) + base64chars.charAt( enc2 );
<span class="lineno" id="44">    44  </span>			output += base64chars.charAt( enc3 ) + base64chars.charAt( enc4 );
<span class="lineno" id="45">    45  </span>		}
<span class="lineno" id="46">    46  </span>		<span class="key">return</span> output;
<span class="lineno" id="47">    47  </span>	}
<span class="lineno" id="48">    48  </span><span class="lineno" id="49">    49  </span>	<span class="comment doc">/**
<span class="lineno" id="50">    50  </span>	 * Decodes a base64 string
<span class="lineno" id="51">    51  </span>	 * @param src (String) - string to decode
<span class="lineno" id="52">    52  </span>	 * @return String
<span class="lineno" id="53">    53  </span>	 */</span>
<span class="lineno" id="54">    54  </span>	<span class="key">public</span> <span class="key">static</span> <span class="key">function</span> decode(src:String):String {
<span class="lineno" id="55">    55  </span>		<span class="key">var</span> i:Number = <span class="num_const">0</span>;
<span class="lineno" id="56">    56  </span>		<span class="key">var</span> output:String = <span class="key">new</span> String( <span class="str_const">""</span> );
<span class="lineno" id="57">    57  </span>		<span class="key">var</span> chr1:Number, chr2:Number, chr3:Number;
<span class="lineno" id="58">    58  </span>		<span class="key">var</span> enc1:Number, enc2:Number, enc3:Number, enc4:Number;
<span class="lineno" id="59">    59  </span>		<span class="key">while</span> (i &lt; src.length) {
<span class="lineno" id="60">    60  </span>			enc1 = base64chars.indexOf( src.charAt( i++ ) );
<span class="lineno" id="61">    61  </span>			enc2 = base64chars.indexOf( src.charAt( i++ ) );
<span class="lineno" id="62">    62  </span>			enc3 = base64chars.indexOf( src.charAt( i++ ) );
<span class="lineno" id="63">    63  </span>			enc4 = base64chars.indexOf( src.charAt( i++ ) );
<span class="lineno" id="64">    64  </span>			chr1 = (enc1 &lt;&lt; <span class="num_const">2</span>) | (enc2 &gt;&gt; <span class="num_const">4</span>);
<span class="lineno" id="65">    65  </span>			chr2 = ((enc2 &amp; <span class="num_const">15</span>) &lt;&lt; <span class="num_const">4</span>) | (enc3 &gt;&gt; <span class="num_const">2</span>);
<span class="lineno" id="66">    66  </span>			chr3 = ((enc3 &amp; <span class="num_const">3</span>) &lt;&lt; <span class="num_const">6</span>) | enc4;
<span class="lineno" id="67">    67  </span>			output += String.fromCharCode( chr1 );
<span class="lineno" id="68">    68  </span>			<span class="key">if</span> (enc3 != <span class="num_const">64</span>) output = output + String.fromCharCode( chr2 );
<span class="lineno" id="69">    69  </span>			<span class="key">if</span> (enc4 != <span class="num_const">64</span>) output = output + String.fromCharCode( chr3 );
<span class="lineno" id="70">    70  </span>		}
<span class="lineno" id="71">    71  </span>		<span class="key">return</span> output;
<span class="lineno" id="72">    72  </span>	}
<span class="lineno" id="73">    73  </span><span class="lineno" id="74">    74  </span>	<span class="key">private</span> <span class="key">function</span> Base64() {
<span class="lineno" id="75">    75  </span>	}
<span class="lineno" id="76">    76  </span>}</pre><ul class="main_nav" id="main_nav"><li><a href="../../../overview-summary.html" title="Overview of SASAPI" class="button">Overview</a></li><li><a href="package-summary.html" title="Overview of package com.sekati.crypt" class="button">Package</a></li><li><a href="Base64.html" title="Detail of com.sekati.crypt.Base64 API" class="button">Class</a></li><li><span class="button nav_current">Source</span></li><li><a href="../../../index-files/index.html" title="Alpabetical index of types and members" class="button">Index</a></li></ul><div class="footer"><a href="http://www.badgers-in-foil.co.uk/projects/as2api/" title="ActionScript 2 API Documentation Generator">as2api</a></div></body></html>